package com.gr.staffpm.projects.service;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gr.staffpm.datatypes.Project;
import com.gr.staffpm.datatypes.Task;
import com.gr.staffpm.datatypes.User;
import com.gr.staffpm.projects.dao.ProjectDAO;

/**
 * Default implementation of the {@link ProjectService} interface.  This service implements
 * operations related to User data.
 */
@Transactional
@Service("projectService")
public class DefaultProjectService implements ProjectService {

    private ProjectDAO projectDAO;

    @Autowired
    public void setProjectDAO(ProjectDAO projectDAO) {
        this.projectDAO = projectDAO;
    }

    @Override
    public void createProject(String name, String desc, Date startDate) {
        Project project = new Project();
        project.setProjectName(name);
        project.setDescription(desc);
        project.setStartDate(startDate);
        projectDAO.createProject(project);
    }

    @Override
    public List<Project> getAllProjects(User user) {
        return projectDAO.getAllProjects(user);
    }

    @Override
    public List<Project> getAllProjects(User user, int first, int count, String property, boolean ascending) {
        return projectDAO.getAllProjects(user, property, ascending).subList(first, first + count);
    }

    @Override
    public Project getProject(int projectId) {
        return projectDAO.getProject(projectId);
    }

    @Override
    public void deleteProject(int projectId) {
        projectDAO.deleteProject(projectId);
    }

    @Override
    public void updateProject(Project project) {
        projectDAO.updateProject(project);
    }

    @Override
    public List<Task> getAllTasks(Project project) {
        return projectDAO.getAllTasks(project, Task.NAME, false);
    }

    @Override
    public List<Task> getTasks(Project project, int first, int count, String property, boolean ascending) {
        List<Task> tasks = projectDAO.getAllTasks(project, property, ascending);
        if ((first + count) > tasks.size()) {
            tasks = tasks.subList(first, tasks.size() - 1);
        } else
            tasks = tasks.subList(first, first + count);
        return tasks;
    }

    @Override
    public List<Task> getTasksCreated(Project project, Date startDate, Date endDate) {
        return projectDAO.getTasksCreatedBetween(project, startDate, endDate);
    }

    @Override
    public List<Task> getTasksResolved(Project project, Date startDate, Date endDate) {
        return projectDAO.getTasksResolvedBetween(project, startDate, endDate);
    }

    @Override
    public Task getTask(int id) {
        return projectDAO.loadTask(id);
    }

    /* (non-Javadoc)
     * @see com.gr.staffpm.projects.service.ProjectService#addProject(com.gr.staffpm.datatypes.Project)
     */
    @Override
    public void addProject(Project newProject) {
        projectDAO.createProject(newProject);
    }

    /* (non-Javadoc)
     * @see com.gr.staffpm.projects.service.ProjectService#checkProjectExists(com.gr.staffpm.datatypes.Project)
     */
    @Override
    public boolean checkProjectExists(Project project) {
        return projectDAO.checkProjectExists(project);
    }

}
